#include "mini2440ChangePassForm.h"
#include "../ui_mini2440ChangePassForm.h"

mini2440ChangePassForm::mini2440ChangePassForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::mini2440ChangePassForm)
{
    ui->setupUi(this);
    this->hide();
    unCurentStep = 0;
}

mini2440ChangePassForm::~mini2440ChangePassForm()
{
    delete ui;
}

void mini2440ChangePassForm::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void mini2440ChangePassForm::on_pushButton_0_clicked()
{
    ui->linePass->insert(ui->pushButton_0->text());
}

void mini2440ChangePassForm::on_pushButton_1_clicked()
{
    ui->linePass->insert(ui->pushButton_1->text());
}

void mini2440ChangePassForm::on_pushButton_2_clicked()
{
    ui->linePass->insert(ui->pushButton_2->text());
}

void mini2440ChangePassForm::on_pushButton_3_clicked()
{
    ui->linePass->insert(ui->pushButton_3->text());
}

void mini2440ChangePassForm::on_pushButton_4_clicked()
{
    ui->linePass->insert(ui->pushButton_4->text());
}

void mini2440ChangePassForm::on_pushButton_5_clicked()
{
    ui->linePass->insert(ui->pushButton_5->text());
}

void mini2440ChangePassForm::on_pushButton_6_clicked()
{
    ui->linePass->insert(ui->pushButton_6->text());
}

void mini2440ChangePassForm::on_pushButton_7_clicked()
{
    ui->linePass->insert(ui->pushButton_7->text());
}

void mini2440ChangePassForm::on_pushButton_8_clicked()
{
    ui->linePass->insert(ui->pushButton_8->text());
}

void mini2440ChangePassForm::on_pushButton_9_clicked()
{
    ui->linePass->insert(ui->pushButton_9->text());
}

void mini2440ChangePassForm::on_checkBox_clicked()
{
    if (ui->checkBox->isChecked())
        ui->linePass->setEchoMode(QLineEdit::Normal);
    else
        ui->linePass->setEchoMode(QLineEdit::Password);
    return;
}

void mini2440ChangePassForm::on_Button_back_clicked()
{
    ui->linePass->backspace();
}

void mini2440ChangePassForm::on_Button_confirm_clicked()
{
    if (unCurentStep == 0)
    {
        if (ui->linePass->text() == qstrCurrentPassword)
            {
                ui->linePass->clear();
                unCurentStep = 1;
                ui->labelPass->setText(QString::fromUtf8("Nhập Mật Khẩu Mới"));
            }
        else
        {
            ui->linePass->clear();
            ui->labelPass->setText("<font color=#FF0000>"+QString::fromUtf8("Sai Mật Khẩu, Mời nhập lại !")+"</font>");
        }
        return;
    }
    else if (unCurentStep == 1)
    {
        if (ui->linePass->text().length() < 8)
        {
            ui->labelPass->setText("<font color=#FF0000>"+QString::fromUtf8("Mật khẩu mới phải có ít nhất 8 ký tự")+"</font>");
            ui->linePass->clear();
        }
        else
        {
            qstrNewPassword = ui->linePass->text();
            unCurentStep = 2;
            ui->labelPass->setText(QString::fromUtf8("Xác nhận lại Mật Khẩu mới"));
            ui->linePass->clear();
        }
    }
    else
    {
        if (ui->linePass->text() == qstrNewPassword)
        {
            ui->labelPass->setText(QString::fromUtf8("Mật Khẩu đổi thành công"));
            ui->linePass->clear();
            unCurentStep = 0;
            config->set_currentPass(qstrNewPassword);
            config->saveConfig();
            qstrCurrentPassword = config->get_currentPass();
            emit saveSetting();
        }
        else
        {
            ui->labelPass->setText("<font color=#FF0000>"+QString::fromUtf8("Xác nhận Mật Khẩu mới không đúng")+"</font>");
            ui->linePass->clear();
        }
    }
}

void mini2440ChangePassForm::setConfig(mini2440Configuration *p_config)
{
    config = p_config;
    qstrCurrentPassword = config->get_currentPass();
}

